import json
from Student import *

class StudentManage():
    def __init__(self):
        self.student_list = []
    def run(self):
        self.load_student()
        while True:
            # 显示菜单
            self.load_menu()
            # 输入功能序号
            menu_num = int(input("请输入需要的功能序号："))
            # 根据序号执行不同的功能
            if menu_num == 1:
                # 添加学员
                self.add_student()
            elif menu_num == 2:
                # 删除学员
                self.del_student()
            elif menu_num == 3:
                # 修改学员
                self.update_student()
            elif menu_num == 4:
                # 查询学员
                self.find_student()
            elif menu_num == 5:
                # 查询所有学员
                self.findAll_student()
            elif menu_num == 6:
                # 存储学员信息
                self.save_student()
            elif menu_num == 7:
                # 退出系统
                break


    def load_student(self):
        '''
        加载学员信息
        :return:
        '''
        print("【加载学员信息】")
        stu_list = []
        with open("stu.data", "r", encoding="utf8") as f:
            for line in f:
                stu_dict = json.loads(line)
                name = stu_dict["name"]
                gender = stu_dict["gender"]
                tel = stu_dict["tel"]
                stu = Student(name=name,gender=gender,tel=tel)
                stu_list.append(stu)
        self.student_list = stu_list
        print("【加载学员信息完成】")
    def save_student(self):
        '''
        存储学员信息
        :return:
        '''
        print("【存储学员信息】")
        with open("stu.data", "w", encoding="utf-8") as f:
            for stu in self.student_list:
                stu_dict = {"name":stu.name,"gender":stu.gender,"tel":stu.tel}
                stu_str = json.dumps(stu_dict,ensure_ascii=False)
                f.write(stu_str)
                f.write("\n")
        print("【存储学员信息完成】")
    def load_menu(self):
        '''
        显示菜单
        :return:
        '''
        print("请选择如下功能：")
        print("1.添加学员")
        print("2.删除学员")
        print("3.修改学员")
        print("4.查询学员")
        print("5.查询所有学员")
        print("6.存储信息")
        print("7.退出系统")

    def add_student(self):
        print("【添加学员】")
        name = input("请输入学员姓名：")
        gender = input("请输入学员性别(男/女)：")
        tel = input("请输入学员号码：")
        stu = Student(name,gender,tel)
        print(f"添加的学员信息：{stu}")
        self.student_list.append(stu)
        print(f"添加后学员列表：{self.student_list}")
    def del_student(self):
        print("【删除学员】")
        name = input("请输入需要删除学员的姓名")
        find_stu = None
        for stu in self.student_list:
            if stu.name == name:
                find_stu = stu
                break
        if find_stu:
            self.student_list.remove(find_stu)
            print("删除学员成功")
        else:
            print("删除学员失败，未找到该学员")

    def update_student(self):
        '''
        修改学员
        :return:
        '''
        print("【修改学员】")
        name = input("请输入需要修改学员的姓名")
        find_stu = None
        index = 0
        for stu in self.student_list:
            if stu.name == name:
                find_stu = stu
                break
            index += 1
        if find_stu:
            name = input("请输入要修改的姓名（不修改按回车）：")
            gender = input("请输入要修改的性别（不修改按回车）：")
            tel = input("请输入要修改的号码（不修改按回车）：")
            if name != "":
                find_stu.name = name
            if gender != "":
                find_stu.gender = gender
            if tel != "":
                find_stu.tel = tel
            self.student_list[index] = find_stu
        else:
            print("修改学员失败，未找到该学员")
    def find_student(self):
        print("【查询学员】")
        name = input("请输入需要查询学员的姓名：")
        find_stu = False
        for stu in self.student_list:
            if stu.name == name:
                print(f"该学员姓名：{stu.name},性别：{stu.gender},电话：{stu.tel}")
                find_stu = stu
                break
        if find_stu == False:
            print("学员查询失败，未找到该学员")
    def findAll_student(self):
        print("【查询所有学员】")
        index = 0
        for stu in self.student_list:
            print(f"第{index}号，{stu}")
            index += 1

